<template>
  <div class="editorDiv">
    <editor :id="id" ref="editorRef" :key="key" v-model="content" :init="init" />
  </div>
</template>

<script>
import tinymce from 'tinymce/tinymce'
import Editor from '@tinymce/tinymce-vue'
import 'tinymce/themes/silver'
import 'tinymce/icons/default/icons'
import 'tinymce/plugins/image'
import 'tinymce/plugins/media'
import 'tinymce/plugins/table'
import 'tinymce/plugins/lists'
import 'tinymce/plugins/contextmenu'
import 'tinymce/plugins/wordcount'
import 'tinymce/plugins/colorpicker'
import 'tinymce/plugins/textcolor'
import 'tinymce/plugins/preview'
import 'tinymce/plugins/code'
import 'tinymce/plugins/link'
import 'tinymce/plugins/advlist'
import 'tinymce/plugins/codesample'
import 'tinymce/plugins/hr'
import 'tinymce/plugins/fullscreen'
import 'tinymce/plugins/textpattern'
import 'tinymce/plugins/searchreplace'
import 'tinymce/plugins/autolink'
import 'tinymce/plugins/directionality'
import 'tinymce/plugins/visualblocks'
import 'tinymce/plugins/visualchars'
import 'tinymce/plugins/template'
import 'tinymce/plugins/charmap'
import 'tinymce/plugins/nonbreaking'
import 'tinymce/plugins/insertdatetime'
import 'tinymce/plugins/imagetools'
import 'tinymce/plugins/autosave'
import 'tinymce/plugins/autoresize'

export default {
  name: 'Index',
  components: {
    Editor
  },
  props: {
    value: {
      type: String,
      default: ''
    },
    height: {
      type: Number,
      default: 600
    },
    id: {
      type: String,
      default: '1tl6wsmzoehu52trbdzz8i7itdp0yy5tk42d1jwo1haunh3h'
    },
    // 默认插件 这里写的比较全，基本上是全部了
    plugins: {
      type: [String, Array],
      default:
        'preview searchreplace autolink directionality visualblocks visualchars fullscreen image link template code codesample table charmap hr nonbreaking insertdatetime advlist lists wordcount imagetools textpattern autosave autoresize'
    },
    // 默认工具栏 这里写的比较全，基本上是全部了
    toolbar: {
      type: [String, Array],
      default:
      // eslint-disable-next-line no-multi-str
        'code undo redo restoredraft | pastetext | forecolor backcolor bold italic underline strikethrough link codesample | alignleft aligncenter alignright alignjustify outdent indent lineheight formatpainter | \
            styleselect formatselect fontselect fontsizeselect | bullist numlist | blockquote subscript superscript removeformat | \
            table image charmap hr pagebreak insertdatetime | bdmap fullscreen preview'
    }
  },
  data() {
    return {
      content: this.value,
      a: {
        href: '',
        title: '',
        target: 0
      },
      imageUrl: '',
      key: this.id,
      dialogVisible: false,
      imageDialogVisible: false,
      // 初始化配置
      init: {
        language_url: 'static/tinymce/skins/langs/zh_CN.js', // 中文语言包路径
        language: 'zh_CN',
        skin_url: 'static/tinymce/skins/ui/oxide',
        height: this.height,
        min_height: this.height,
        max_height: this.height,
        toolbar_mode: 'wrap',
        plugins: this.plugins,
        toolbar: this.toolbar,
        content_style: 'p {margin: 5px 0;}',
        fontsize_formats: '12px 14px 16px 18px 24px 36px 48px 56px 72px',
        font_formats:
          '微软雅黑=Microsoft YaHei,Helvetica Neue,PingFang SC,sans-serif;苹果苹方=PingFang SC,Microsoft YaHei,sans-serif;宋体=simsun,serif;仿宋体=FangSong,serif;黑体=SimHei,sans-serif;Arial=arial,helvetica,sans-serif;Arial Black=arial black,avant garde;Book Antiqua=book antiqua,palatino;',
        branding: false,
        images_upload_handler: (blobInfo, success) => {
          const img = 'data:image/jpeg;base64,' + blobInfo.base64()
          success(img)
        }
      }
    }
  },
  watch: {
    value(newValue) {
      console.log(newValue, 'value')
      this.content = newValue
    },
    content(newValue) {
      console.log(newValue, 'content')
      this.$emit('input', newValue)
    }
  },
  mounted() {
    tinymce.init({})
  },
  methods: {
  }
}
</script>

<style scoped>
.editorDiv {
  display: flex;
  flex-direction: row-reverse;
}

.uploadDiv {
  position: absolute;
  margin-top: 0;
  flex-direction: column;
  z-index: 999;
}

.upload-demo {
  display: flex;
  flex-direction: row-reverse;
}

.upload-demo2 {
  display: flex;
  flex-direction: row;
  justify-content: center;
  align-items: center;
}

.input_select {
  display: flex;
  flex-direction: row;
  justify-content: center;
  padding: 5px;
}
</style>
